package com.example.drss;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.example.drss.db.DBTool;
import com.example.drss.fragment.FourFragment;
import com.example.drss.fragment.OneFragment;
import com.example.drss.fragment.ThreeFragment;
import com.example.drss.fragment.TwoFragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class DRSSActivity extends FragmentActivity {

	final static String DB_NAME="menu.sqlite";
	final static String TABLE_NAME="menu";

	DBTool dbTool;
	

	ApplicationInfo applicationInfo;
	String packageName;
	public String databasePath;

	RadioGroup radioGroup;
	FragmentManager fragmentManager;
	FragmentTransaction fragmentTransaction;
	OneFragment oneFragment;
	TwoFragment twoFragment ;
	ThreeFragment threeFragment;
	FourFragment fourFragment;

	RadioButton oneRadioButton;
	RadioButton twoRadioButton;
	RadioButton threeRadioButton;
	RadioButton fourRadioButton;
	
	public String user;

	OnCheckedChangeListener listener=new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method 

			//显示或隐藏 Fragment
			fragmentTransaction=fragmentManager.beginTransaction();
			switch (checkedId) {
			case R.id.radio_one:
				oneRadioButton.setBackgroundResource(R.drawable.radio_bg_checked);
				twoRadioButton.setBackgroundResource(R.drawable.radio_bg);
				threeRadioButton.setBackgroundResource(R.drawable.radio_bg);
				fourRadioButton.setBackgroundResource(R.drawable.radio_bg);
				fragmentTransaction.show(oneFragment);
				fragmentTransaction.hide(twoFragment);
				fragmentTransaction.hide(threeFragment);
				fragmentTransaction.hide(fourFragment);
				break;
			case R.id.radio_two:
				oneRadioButton.setBackgroundResource(R.drawable.radio_bg);
				twoRadioButton.setBackgroundResource(R.drawable.radio_bg_checked);
				threeRadioButton.setBackgroundResource(R.drawable.radio_bg);
				fourRadioButton.setBackgroundResource(R.drawable.radio_bg);
				fragmentTransaction.hide(oneFragment);
				fragmentTransaction.show(twoFragment);
				fragmentTransaction.hide(threeFragment);
				fragmentTransaction.hide(fourFragment);
				break;
			case R.id.radio_three:
				oneRadioButton.setBackgroundResource(R.drawable.radio_bg);
				twoRadioButton.setBackgroundResource(R.drawable.radio_bg);
				threeRadioButton.setBackgroundResource(R.drawable.radio_bg_checked);
				fourRadioButton.setBackgroundResource(R.drawable.radio_bg);
				fragmentTransaction.hide(oneFragment);
				fragmentTransaction.hide(twoFragment);
				fragmentTransaction.show(threeFragment);
				fragmentTransaction.hide(fourFragment);
				break;
			case R.id.radio_four:
				oneRadioButton.setBackgroundResource(R.drawable.radio_bg);
				twoRadioButton.setBackgroundResource(R.drawable.radio_bg);
				threeRadioButton.setBackgroundResource(R.drawable.radio_bg);
				fourRadioButton.setBackgroundResource(R.drawable.radio_bg_checked);
				fragmentTransaction.hide(oneFragment);
				fragmentTransaction.hide(twoFragment);
				fragmentTransaction.hide(threeFragment);
				fragmentTransaction.show(fourFragment);
				break;
			default:
				break;
			}

			fragmentTransaction.commit();

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drss);
		initDatabase();  
		dbTool=new DBTool(databasePath);
		dbTool.openDatabase();
		Intent intent=getIntent();
		user=intent.getStringExtra("user");
		if(user.equals("")){
			user="未登录";
		}else {
			user="当前用户:"+intent.getStringExtra("user");
			
		}
		
		init();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dbTool.closeDatabase();
	}

	private void init(){
		radioGroup=(RadioGroup)findViewById(R.id.radiogroup);
		radioGroup.setOnCheckedChangeListener(listener);

		oneRadioButton=(RadioButton)findViewById(R.id.radio_one);
		twoRadioButton=(RadioButton)findViewById(R.id.radio_two);
		threeRadioButton=(RadioButton)findViewById(R.id.radio_three);
		fourRadioButton=(RadioButton)findViewById(R.id.radio_four);

		oneRadioButton.setBackgroundResource(R.drawable.radio_bg_checked);
		twoRadioButton.setBackgroundResource(R.drawable.radio_bg);
		threeRadioButton.setBackgroundResource(R.drawable.radio_bg);
		fourRadioButton.setBackgroundResource(R.drawable.radio_bg);

		//动态（程序）添加Fragment
		fragmentManager=getSupportFragmentManager();

		fragmentTransaction=fragmentManager.beginTransaction();

		oneFragment=new OneFragment();
		twoFragment=new TwoFragment();
		threeFragment=new ThreeFragment();
		fourFragment=new FourFragment();

		fragmentTransaction.add(R.id.framelayout_hoder, oneFragment, "one");
		fragmentTransaction.add(R.id.framelayout_hoder, twoFragment, "two");
		fragmentTransaction.add(R.id.framelayout_hoder, threeFragment, "three");
		fragmentTransaction.add(R.id.framelayout_hoder, fourFragment, "four");

		fragmentTransaction.show(oneFragment);
		fragmentTransaction.hide(twoFragment);
		fragmentTransaction.hide(threeFragment);
		fragmentTransaction.hide(fourFragment);
		fragmentTransaction.commit();

	}

	private void initDatabase(){
		packageName=getPackageName();
		try {
			applicationInfo=getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
			String databaseDir=applicationInfo.dataDir+File.separator+"database";
			databasePath=databaseDir+File.separator+DB_NAME;
			//Toast.makeText(DRSSActivity.this, databasePath, Toast.LENGTH_SHORT).show();
			File dbDir=new File(databaseDir);
			if(!dbDir.exists()){
				dbDir.mkdir();
			}
			InputStream inputStream=getAssets().open("menu.sqlite");
			File dbFile=new File(databasePath);
			if(!dbFile.exists()){
				dbFile.createNewFile();
				FileOutputStream fos=new FileOutputStream(dbFile);
				byte[] buffer= new byte[1024];
				int count=0;
				while((count=inputStream.read(buffer))!=-1){
					fos.write(buffer, 0, count);
				}
				fos.close();
				inputStream.close();
			}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)  
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK )  
        {  
        	new AlertDialog.Builder(DRSSActivity.this).setTitle("退出提示").setMessage("是否退出？")
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				finish();				
			}})
			.setNegativeButton("取消",null)
			.show(); 
        }          
        return false;  
          
    }  
  
	
	public Cursor getMenuSeller(){
		return dbTool.getMenuSeller();
		//return database.rawQuery("select seller from menu group by seller;",null);
	}
	public Cursor getAll(){
		return dbTool.getAll();
	}
	public Cursor query(String name){
		return dbTool.query(name);
	}
}
